package com.zhf.server.impl;

import com.zhf.model.*;
import com.zhf.server.Api;
import com.zhf.server.ServerDao;
import com.zhf.server.TraineeService;

import java.util.List;

@Api("/trainee")
public class TrainServiceImpl implements TraineeService {
    ServerDao serverDao;

    public TrainServiceImpl(ServerDao serverDao){
        this.serverDao = serverDao;
    }

    @Override
    @Api("/addTrainee")
    public UserMessage addTrainee(UserMessage userMessage) {
        // 查找添加的用户账号是否存在
        User user = userMessage.getUser();
        UserMessage userMessage1 = serverDao.addTrainee(user);
        return userMessage1;
    }

    @Override
    @Api("/deleteTrainee")
    public UserMessage deleteTrainee(User user) {
        UserMessage userMessage = selectTrainee(user);
        if("fail".equals(userMessage.getType())){
            return  userMessage;
        }
        else{
            userMessage = serverDao.deleteTrainee(user);
        }
        return userMessage;
    }

    @Override
    @Api("/updateTrainee")
    public UserUpdVo updateTrainee(UserUpdVo userUpdVo) {
        UserUpdVo userUpdVo1 = new UserUpdVo();
        String originUserAccount = userUpdVo.getOriginUserAccount();
        User user1 = new User();
        user1.setUserName(originUserAccount);
        UserMessage userMessage = selectTrainee(user1);
        if("fail".equals(userMessage.getType())){
            UserMessage userMessage1 = new UserMessage();
            userMessage1.setUser(userUpdVo.getUserMessage().getUser());
            userMessage1.setType("fail");
            userUpdVo1.setUserMessage(userMessage1);
            userUpdVo1.setOriginUserAccount(originUserAccount);
            return userUpdVo1;
        }
        else{
            userUpdVo1 = serverDao.updateTrainee(userUpdVo);
        }
        return userUpdVo1;
    }

    @Override
    @Api("/selectTrainee")
    public UserMessage selectTrainee(User user) {
        UserMessage userMessage = serverDao.selectTrainee(user);
        return userMessage;
    }

    @Override
    @Api("/iterator")
    public List<User> listUser() {
        List<User> userList = serverDao.listUser();
        return userList;
    }

    @Override
    @Api("/updatePassword")
    public UserPasswordMessage updatePassword(UserPassword userPassword) {
        UserPasswordMessage userPasswordMessage = new UserPasswordMessage();
        userPasswordMessage.setUserPassword(userPassword);
        boolean b = serverDao.serverUserCheck(new User(userPassword.getUserName(), userPassword.getOldPassword()));
        if(b){
            userPasswordMessage = serverDao.updateTraineePassword(userPassword);
        }else{
            userPasswordMessage.setMessage("fail");
        }
        return userPasswordMessage;
    }


}
